package com.pickgliss.render2d.vo
{
	import com.pickgliss.render2d.events.MeEvent;
	
	import flash.events.ErrorEvent;
	import flash.events.EventDispatcher;
	import flash.utils.ByteArray;
	[Event(name="addLayer", type="com.pickgliss.render2d.events.MeEvent")]
	[Event(name="removeLayer", type="com.pickgliss.render2d.events.MeEvent")]
	public class MeInfo extends EventDispatcher implements IReadWriteAble
	{
		public var name:String = "";
		public var width:int;
		public var height:int;
		public var type:String = "";
		public var layers:Vector.<LayerInfo> = new Vector.<LayerInfo>();
		public var savedLocation:String = "";
		
		public function toBytearray():ByteArray
		{
			var result:ByteArray = new ByteArray();
			result.writeUTF(name);
			result.writeInt(width);
			result.writeInt(height);
			result.writeUTF(type);
			result.writeUTF(savedLocation);
			result.writeInt(layers.length);
			for(var i:int = 0;i<layers.length;i++)
			{
				var item:ByteArray = layers[i].toBytearray();
				result.writeBytes(item,0,item.length);
			}
			return result;
		}
		
		public function addLayer(layer:LayerInfo):void
		{
			layers.push(layer);
			dispatchEvent(new MeEvent(MeEvent.LAYER_ADD,layer));
		}
		
		public function readFromBytearray(b:ByteArray):void
		{
			name = b.readUTF();
			width = b.readInt();
			height = b.readInt();
			type = b.readUTF();
			savedLocation = b.readUTF();
			var count:int = b.readInt();
			for(var i:int = 0;i<count;i++)
			{
				var item:LayerInfo = new LayerInfo();
				item.readFromBytearray(b);
				addLayer(item);
			}
		}
	}
}